package com.kh.reggie.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;


import java.util.Random;

/**
 * 发送邮件验证码
 */
@Component
@Slf4j
public class EmailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 读取yml文件中username的值并赋值给sendUserEmail
     */
    @Value("${spring.mail.username}")
    private String sendUserEmail;

    /**
     * 发送邮件验证码
     * @param emailReceiver
     * @return
     */
    public String sendSimpleMail(String emailReceiver,String code) {
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件发送者
        message.setFrom(sendUserEmail);
        // 设置邮件接收者
        message.setTo(emailReceiver);
        // 设置邮件的主题
        message.setSubject("登录验证码");
        // 设置邮件的正文
//        Random random = new Random();
//        StringBuilder code = new StringBuilder();
//        for (int i = 0; i < 6; i++) {
//            int r = random.nextInt(10);
//            code.append(r);
//        }
        String text = "您的验证码为：" + code + ",请勿泄露给他人。";
        message.setText(text);
        // 发送邮件
        try {
            javaMailSender.send(message);
            log.info("邮件发送成功");
            return ("邮件发送成功");
        } catch (MailException e) {
            e.printStackTrace();
            log.info("邮件发送失败");
            return ("邮件发送失败");
        }
    }
}

